/*
 * UI_Keys.c
 *
 *  Created on: 23.02.2011
 *      Author: Tavin
 */

#include <stdint.h>

#include "board.h"
#include "gpio.h"

#include "boolean.h"
#include "UIKeys.h"


int ui_keys []= {KEY_L_UP, KEY_L_DOWN, KEY_L_LEFT,KEY_L_RIGHT,
				KEY_R_UP, KEY_R_DOWN, KEY_R_LEFT,KEY_R_RIGHT };


UiKeysData_t UiKeysData;


/****************************************************************************************/
/*																						*/
/*	Function : UIKeys_init									Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.2.23			Status : Working   			*/
/*																						*/
/****************************************************************************************/
void UIKeys_init()
{
	int counter;

	for (counter=0;counter<KEY_COUNT; counter++)
		{
		gpio_enable_gpio_pin(ui_keys[counter]);
		gpio_enable_pin_pull_up(ui_keys[counter]);
		gpio_enable_pin_glitch_filter(ui_keys[counter]);
		}

	UiKeysData.keyStatus=0;
	UiKeysData.keyFlank=0;
}

/****************************************************************************************/
/*																						*/
/*	Function : UIKeys_process								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.3.27			Status : Working   			*/
/*																						*/
/****************************************************************************************/
void UIKeys_process()
{

	uint8_t counter;

	for (counter=0; counter<KEY_COUNT;counter++)
		{
		if (gpio_get_pin_value(ui_keys[counter])>0)
			{
			UiKeysData.keyStatus&=~(1<<counter);
			UiKeysData.keyFlank&=~(1<<counter);
			}
		else
			{
			if ((UiKeysData.keyStatus& (1<<counter))==0)
					{
					UiKeysData.keyFlank|=(1<<counter);
					UiKeysData.keyStatus|=(1<<counter);
					}

			}
		}

}


/****************************************************************************************/
/*																						*/
/*	Function : UIKeys_keyPressed							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.3.27			Status : Working   			*/
/*																						*/
/****************************************************************************************/

boolean UIKeys_keyPressed(uint8_t keyNumber)
{
	if (keyNumber<KEY_COUNT)
		if ((UiKeysData.keyFlank& (1<<keyNumber))>0)
			{
			UiKeysData.keyFlank&=~(1<<keyNumber);
			return(true);
			}

	return(false);
}
